package leetcode;

class 岛屿的周长 {

    public static void main(String[] args) {

        int res = islandPerimeter(new int[][]{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}});
        System.out.println(res);
    }

    public static int islandPerimeter(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0)
                    continue;
                if (i == 0 || grid[i - 1][j] == 0)
                    ans++;
                if (i == m - 1 || grid[i + 1][j] == 0)
                    ans++;
                if (j == 0 || grid[i][j - 1] == 0)
                    ans++;
                if (j == n - 1 || grid[i][j + 1] == 0)
                    ans++;
            }

        }
        return ans;
    }
}
